<template>
  <a-modal
    :visible="visible"
    :title="t('edit')"
    :cancelText="t('cancel')"
    :okText="t('submit')"
    @ok="submit"
    @cancel="cancel(false)"
  >
    <a-form
       ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
    
      <a-form-item :label="t('higher')">
        <a-tree-select
          v-model:value="formState.parent"
          style="width: 100%;"
          :dropdown-style="{ maxHeight: '360px', overflow: 'auto' }"
          :tree-data="state.powers"
          :placeholder="t('higher power')"
          replace
          tree-default-expand-all
          :fieldNames="fieldNames" 
        >
        </a-tree-select>
      </a-form-item>
      <a-form-item ref="title" :label="t('title')" name="title">
        <a-input v-model:value="formState.title" />
      </a-form-item> 
      <a-form-item  ref="path" :label="t('path')" name="path">
        <a-input v-model:value="formState.path" />
      </a-form-item>
      <a-form-item  ref="name" :label="t('name')" name="name">
        <a-input v-model:value="formState.name" />
      </a-form-item>
      <a-form-item  :label="t('icon')"  ref="icon" >
        <go2-icon-picker v-model="formState.icon"></go2-icon-picker>
      </a-form-item>
      <a-form-item ref="i18n" :label="t('i18n')" name="i18n">
        <a-input v-model:value="formState.i18n" />
      </a-form-item>
      <a-form-item ref="type" :label="t('type')" name="type">
        <a-select v-model:value="formState.type">
          <a-select-option value="dir"> 目录 </a-select-option>
          <a-select-option value="menu"> 菜单 </a-select-option>
          <a-select-option value="button"> 按钮 </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item ref="sort" :label="t('sort')" name="sort">
        <a-input-number v-model:value="formState.sort" />
      </a-form-item>
      <a-form-item :label="t('status')" name="status">
        <a-switch :checkedValue="1" :unCheckedValue="0" :checkedChildren="t('open')" :unCheckedChildren="t('close')" v-model:checked="formState.status" />
     
      </a-form-item>
      <!-- <a-form-item label="备注" name="remark">
        <a-textarea v-model:value="formState.remark" />
      </a-form-item> -->
    </a-form>
  </a-modal>
</template>
<script>
import { useI18n  } from "vue-i18n";
import { message } from 'ant-design-vue';
import { edit, tree } from "@/core/api/module/permission";
import { defineComponent, reactive, ref, toRaw, watch } from "vue";
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
    record: {
      type: Object
    }
  },
  emit: ["close"],
  setup(props, context) {
    const { t } = useI18n();
    const state = reactive({
      depts: [],
      showComponent: true,
      showPath: true,
      showCode: false
    });
    
    const formRef = ref();
    
    let formState = reactive({ 
    });

    
    watch(props,(props) => {   
       
      formState.id = props.record.id;
      formState.title = props.record.title;
      formState.name = props.record.name;
      formState.path = props.record.path; 
      formState.status = props.record.status ; 
      formState.i18n = props.record.i18n;
      formState.type = props.record.type;
      formState.icon = props.record.icon;
      formState.sort = props.record.sort; 
      formState.parent = props.record.parent; 
     
    })

    const formRules = {
      title: [{ required: true, message: '请输入权限名称', trigger: 'blur'}],
      i18n: [{ required: true, message: '请输入 i18n', trigger: 'blur'}],
      type: [{ required: true, message: '请输入权限类型', trigger: 'blur'}],
      parent: [{ required: true, message: '请选择上级权限', trigger: 'change'}]
    };
 

    const submit = (e) => {
      formRef.value
        .validate()
        .then(() => {
          edit(toRaw(formState)).then((response)=>{
              if(response.code == 0){
                message.success({ content: '保存成功', duration: 1 }).then(()=>{
                  cancel(true);
                });
              }else{
                message.error({ content: response.msg, duration: 1 }).then(()=>{
                  cancel(false);
                });
              }
          });
        })
        .catch(error => {
          console.log('error', error);
        });
    };

     
    const cancel = (e=false) => {
      formRef.value.resetFields();
      context.emit("close", e);
    };

    const loadPower = () => {
      tree({}).then((response)=>{
        response.data = [{
          id:0,
          title: "顶级菜单",
          children: response.data
        }]
        state.powers = response.data;
      })
    }

    loadPower();

    return {
      t,
      state,
      submit, 
      cancel,
      formRef,
      formState,
      formRules,
      
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },

      fieldNames: {children:'children', title:'title', key:'id', value: 'id' }
    };
  },
});
</script>